import utils.generator as gen
from utils.config import Config


class SetYmlData:

    @staticmethod
    def set_yml_data_by_ui(test_self, action_list):
        """将yml数据配置到ui页面上"""
        for action in action_list:
            method = action.get("find_method")
            # 先定位元素位置
            test_self.page.element = test_self.page.switch_method_yml.get(method)(test_self.page, action)
            # 根据配置文件设定的动作以及set值
            test_self.page.switch_type_yml.get(action.get("type"))(test_self.page, action)

    def convert_yml_data_by_http(self, params):
        """
        将yml的param参数进行转换
        :param params: yml文件里面的参数param
        :return:
        """
        if params:
            for d, x in params.items():
                value = self.set_gen_data(str(x))
                # 把转换好的值，替换成原来的值
                if value not in "nothing":
                    params[d] = value
        return params

    def set_gen_data(self, argument):
        """
        根据argument调用对应当前class里面方法
        :param argument: 调用的方法名称
        :return:
        """
        method = getattr(self, argument, lambda: "nothing")
        return method()

    @staticmethod
    def random_phone_number():
        return gen.random_phone_number()

    @staticmethod
    def random_name():
        return gen.random_name()

    @staticmethod
    def random_address():
        return gen.random_address()

    @staticmethod
    def random_email():
        return gen.random_email()

    @staticmethod
    def random_ipv4():
        return gen.random_ipv4()

    @staticmethod
    def random_vehicle_num():
        return gen.random_vehicle_num()

    @staticmethod
    def random_id_card():
        return gen.random_id_card()


if __name__ == '__main__':
    yml_data = Config(Config.get_yml_file_path("test_proof_data_http.yml"))
    test_data = yml_data.get("information_list").get("test_list")[1]
    convert_param = SetYmlData().convert_yml_data_by_http(test_data.get("param"))
    print(str(convert_param))
